$(function() {
    var taskTable = null;
    var opts = {
        targetId: "taskTable",
        templateId: "tabletask",
        settings: {
            searchParam: {

            },
            url: DI.queryTask,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            }
        },
        customFuncs: {
            delTaskPop: function(row) {
                mainCtrl.deltaskNo = row.taskNo;
                mainCtrl.deltaskTopic = row.taskTopic;
                $("#popup_del").show();
            },
            selAllcheckbox: function() {
                var curCheckboxs = $("table tbody td input[type=checkbox]");
                for (var i = 0; i < curCheckboxs.length; i++) {
                    if ($(curCheckboxs[i]).attr("disabled") != "disabled") {
                        if ($("#checkTop")[0].checked) {
                            curCheckboxs[i].checked = true;
                        } else {
                            curCheckboxs[i].checked = false;
                        }
                    }
                }
            }
        }
    };
    //实例化数据表，获取主推任务列表信息
    taskTable = SwiftUI.swiftTable.getInstance(opts);

    var mainCtrl = avalon.define({
        $id: "mainController",
        taskTopic: "",
        startTime: "",
        endTime: "",
        deltaskNo: "",
        deltaskTopic: "",
        searchTask: function() {
            var searchParam = {
                "endTime": mainCtrl.endTime,
                "startTime": mainCtrl.startTime,
                "taskTopic": mainCtrl.taskTopic
            }
            taskTable.search(searchParam);
        },
        exportTask: function() {
            var taskNos = [];
            var curCheckboxs = $("table tbody td input[type=checkbox]");
            for (var i = 0; i < curCheckboxs.length; i++) {
                if (curCheckboxs[i].checked) {
                    taskNos.push($(curCheckboxs[i]).val());
                }
            }
            if (taskNos.length == 0) {
                PLATFORM.showTips("warning", "请先选择任务再进行导出");
                return;
            }
            var stringtaskNos = JSON.stringify(taskNos);
            window.location.href = DI.hqExportCompltInfo + "?taskNos=" + stringtaskNos;
        }
    });

    var popCtrl = avalon.define({
        $id: "popController",
        delData: function() {
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "taskNo": mainCtrl.deltaskNo,
                    "taskTopic": mainCtrl.deltaskTopic
                },
                url: DI.deleteTask,
                success: function(data) {
                    $("#popup_del").hide();
                    if (data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        taskTable.searchParam.pageNo = "1";
                        taskTable.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    });

    PLATFORM.datepickerRange("#starttime", "#endtime");

    avalon.scan();
});

/*
 * 分配状态  过滤器
 * 0未分配,1已分配;2修改待分配
 */
avalon.filters.filterAssignStatus = function(str, args, args2) {
    var ret = "未分配";
    if (str == 0) {
        ret = "未分配"
    } else if (str == 2) {
        ret = "已分配";
    } else if (str == 1) {
        ret = "修改待分配";
    }
    return ret;
};
/*
 * 任务状态 过滤器
 * 0 未开始 ,1 进行中,2 已结束,3 已删除;
 */
avalon.filters.filterTaskStatus = function(str, args, args2) {
    var ret = "未开始";
    if (str == 0) {
        ret = "未开始"
    } else if (str == 1) {
        ret = "进行中";
    } else if (str == 2) {
        ret = "已结束";
    } else if (str == 3) {
        ret = "已删除";
    }
    return ret;
};